<?php

/**
 * IndexController - The default controller class
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class Adminb_VirtualController extends Zend_Controller_Action 
{
	

	
	public function init(){
		
		$member=new CMember();
		
		if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName())){
			
			if($_COOKIE[COOKIE_PRE.'uid']!=''){
				
				$this->_redirect(MY_URL.'/seller');
				
			}else{
			
				$this->_redirect(MY_URL.'/seller-auth-login');
				
			}
			
		}
		
	}
	
	public function preDispatch()
	{

		$this->getHelper ('layout')->setLayout('framelayout');
		
		$this->view->addHelperPath(MY_ROOT.'/vhelpers');
		
		Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');

	}
	
	public function exchangeAction(){
		
		$this->_helper->viewRenderer->setNoRender();
		
		$couponMoney=$this->_request->getParam('coupon');
		
		$type=$this->_request->getParam('type');
		
		$member=new CMember();
		
		$memberArray=$member->getUserDetails(array('uid'=>$_COOKIE[COOKIE_PRE.'uid']));
		
		if($type=='toCoupon'){
			
			$virtualMoney=$this->_request->getParam('toCoupon');
		
			if($memberArray[0]['virtual']<$virtualMoney){
				
				$this->view->showMsg("对不起，您的虚拟货币不足！",$_SERVER['HTTP_REFERER']);
				
			}else{
			
				$virtual=new CVirtual();
				
				$virtual_exchange_rate=$virtual->getDetail('virtual_exchange_rate');
				
				$couponMoney=$virtualMoney/$virtual_exchange_rate;
				
				//$member->updateCoupon($coupon,$_COOKIE[COOKIE_PRE.'uid'],true);
				
				$mycoupon=new CCoupon();
				
				$mycoupon->addCoupon($couponMoney,"WHERE uid='".$_COOKIE[COOKIE_PRE.'uid']."'");
				
				$virtual->subVirtual($virtualMoney,"WHERE uid='".$_COOKIE[COOKIE_PRE.'uid']."'");
				
				$this->view->showMsg("兑换成功！",$_SERVER['HTTP_REFERER']);
				
			}
		
		}else if($type=='toVirtual'){
			
			$couponMoney=$this->_request->getParam('toVirtual');
			
			if($memberArray[0]['my_coupon']<$couponMoney){
				
				$this->view->showMsg("对不起，您的消费券余额不足！",$_SERVER['HTTP_REFERER']);
				
			}else{
			
				$virtual=new CVirtual();
				
				$virtual_exchange_rate=$virtual->getDetail('virtual_exchange_rate');
				
				$virtualMoney=$couponMoney*$virtual_exchange_rate;
				
				//$member->updateCoupon($coupon,$_COOKIE[COOKIE_PRE.'uid'],true);
				
				$mycoupon=new CCoupon();
				
				$mycoupon->subCoupon($couponMoney,"WHERE uid='".$_COOKIE[COOKIE_PRE.'uid']."'");
				
				$virtual->addVirtual($virtualMoney,"WHERE uid='".$_COOKIE[COOKIE_PRE.'uid']."'");
				
				$this->view->showMsg("兑换成功！",$_SERVER['HTTP_REFERER']);
				
			}
			
		}
		
	}
    
	
	public function postAction(){
	
		//$this->_helper->viewRenderer->setNoRender();
		
		//$this->getHelper ('layout')->disableLayout ();
	
	}
    

    
}
